package net.sudot.chess.listener.game;

import net.sudot.chess.business.model.WechatGroup;
import net.sudot.chess.business.service.WechatGroupService;
import net.sudot.chess.event.game.ClosedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 游戏结束事件
 *
 * @author zhanyunmeng on 2018-07-14.
 */
@Component
public class EndedEventListener implements ApplicationListener<ClosedEvent> {

    @Resource
    private WechatGroupService wechatGroupService;

    @Override
    public void onApplicationEvent(ClosedEvent endedEvent) {
        Long wechatGroupId = endedEvent.getRoom().getWechatGroupId();
        WechatGroup wechatGroup = wechatGroupService.find(wechatGroupId);
        if (wechatGroup != null) {
            wechatGroup.setUsed(false);
            wechatGroupService.update(wechatGroup);
        }
    }
}
